import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'

import Notfound from '@/components/notfound/Notfound'
import Home from '@/pages/home/Home'
import Nearby from '@/pages/nearby/Nearby'
import Dynamic from '@/pages/dynamic/Dynamic'
import Find from '@/pages/find/Find'
import User from '@/pages/user/User'
import Start from '@/components/Start'

Vue.use(Router)

const routes = [
  {
    path: '*',
    component:Notfound
  },
  {
    path: '/',
    name: 'Start',
    component: Start
  },
  {
    path: '/home',
    name: 'home',
    component: Home,
    meta:{
      requireAuth: true,
    }
  },
  {
    path: '/nearby',
    name: 'nearby',
    component: Nearby
  },
  {
    path: '/dynamic',
    name: 'dynamic',
    component: Dynamic
  },
  {
    path: '/find',
    name: 'find',
    component: Find
  },
  {
    path: '/user',
    name: 'user',
    component: User
  }
];

const router = new Router({
  mode:'history',
  routes
})


// if (window.localStorage.getItem("token")) {
//   store.commit(types.LOGIN, window.localStorage.getItem('token'))
// }

router.beforeEach((to, from, next) => {
  let token = window.localStorage.getItem('token')

  if (to.matched.some(record => record.meta.requiresAuth)) {
      console.log(2);
      if (store.state.token) {
          next()
      } else {
          next({
              path: '/login',
              query: { redirect: to.fullPath }
          })
      }
  } else {
      next()
  }
})

export default router